package cn.weisoft.redisdemo.controller;

import cn.weisoft.redisdemo.model.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.*;

@RestController
@RequestMapping("/redis/")
public class RedisController {

    @Autowired
    private RedisTemplate redisTemplate = null;

    @Value("${weisoft.topicname1}")
    private String topicName01;

    @Value("${weisoft.topicname2}")
    private String topicName02;

    @PostConstruct
    public void init() {
        initRedisTemplate();
        System.out.println("PostConstruct");
    }

    private void initRedisTemplate() {
        RedisSerializer stringSerializer = redisTemplate.getStringSerializer();
        redisTemplate.setKeySerializer(stringSerializer);
        //redisTemplate.setValueSerializer(stringSerializer);
        redisTemplate.setHashKeySerializer(stringSerializer);
        //redisTemplate.setHashValueSerializer(stringSerializer);

    }

    @GetMapping("/test")
    public String test() {

        System.out.println("topicName:" + topicName01);
        String ret = "";
        redisTemplate.opsForValue().set("name", "wilson.fu");
        redisTemplate.opsForValue().set("name2", "傅威");

        ret = (String) redisTemplate.opsForValue().get("name2");

        Book book = new Book();
        book.setId(1);
        book.setName("深入浅出Spring");
        book.setPublishDate(new Date());

        redisTemplate.opsForValue().set("book_1", book);

        Book newBook = (Book) redisTemplate.opsForValue().get("book_1");

        System.out.println(newBook);
        return ret;
    }


    @GetMapping("/sendMsg")
    public void sendMsg() {

        Book book = new Book();
        book.setId(1);
        book.setName("深入浅出Spring");
        book.setPublishDate(new Date());
        System.out.println("Send Msg:" + book.toString());
        redisTemplate.convertAndSend(topicName01, "   " + book.toString());

        redisTemplate.convertAndSend(topicName01, "测试消息1");
        redisTemplate.convertAndSend("topicName02", "测试消息2");
    }

    @RequestMapping("/lua2")
    public Map<String, Object> testLua2(String key1, String key2, String value1, String value2) {
        String lua = " redis.call('set',KEYS[1],ARGV[1])\n"
                + " redis.call('set',KEYS[2],ARGV[2]) \n"
                + " local str1 = redis.call('get',keys[1]) \n"
                + " local str2 = redis.call('get',KEYS[2]) \n"
                + " if str1==str2 then \n "
                + " return 1 \n "
                + "  end \n "
                + " return 0 \n";
        System.out.println(lua);

        DefaultRedisScript<Long> rs = new DefaultRedisScript();
        rs.setScriptText(lua);
        rs.setResultType(Long.class);

        RedisSerializer<String> stringRedisSerializer = redisTemplate.getStringSerializer();
        List<String> keyList = new ArrayList();
        keyList.add(key1);
        keyList.add(key2);
        Long result = (Long) redisTemplate.execute(rs, stringRedisSerializer, stringRedisSerializer, keyList, value1, value2);
        Map<String, Object> map = new HashMap();
        map.put("result", result);
        return map;
    }
}
